//
//  TFQuestionCell.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/4/19.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFQuestionCell: UITableViewCell {
    @objc public let nameLab = UILabel()
    @objc public let avater = UIImageView()
    @objc public var cellwidth:CGFloat = 0

    @objc public var model:TFQuestionData?
    {
        didSet{
             if(model != nil)
            {
                 self.setModelUi(model: model!)
             }
        }
    }
    @objc public var isQuestion:Bool = false
    //回复
    @objc public let replyView = UIView()

    //YES是横屏;NO为竖屏
    @objc public var isOrientationLandscape:Bool = false

    //区分提问记录和互动区域
    @objc public var isChatArea:Bool = false

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.backgroundColor = UIColor.clear

        self.replyView.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 0.8)
       
        self.replyView.layer.cornerRadius = 14
        self.replyView.layer.masksToBounds = true

        self.contentView.addSubview(self.replyView)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setModelUi(model:TFQuestionData)
    {
      
        var nickNameColor = UIColor.white
        var msgColor = UIColor.black
        
        
        //回复的人 的内容颜色
        var replymsgColor = UIColor.black
        //回复的人 的名字颜色
        var replyNicknameColor = UIColor.white

        
        if (self.isOrientationLandscape==false) { //竖屏
            self.replyView.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 0.8)
            msgColor = UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1)
            if (model.answer.count>0) { // 回复
                
                nickNameColor = UIColor(red: 168/255.0, green: 178/255.0, blue: 188/255.0, alpha: 0.6)
               
              
                replyNicknameColor = UIColor(red: 150/255.0, green: 160/255.0, blue: 170/255.0, alpha: 1)
             
            } else {
                nickNameColor = UIColor(red: 150/255.0, green: 160/255.0, blue: 170/255.0, alpha: 1)
               
            }

        } else {
            if (self.isChatArea) {
                self.replyView.backgroundColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.5)
       
                msgColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
           
                nickNameColor = UIColor(red: 65/255.0, green: 130/255.0, blue: 235/255.0, alpha: 1)
             

                replyNicknameColor = UIColor(red: 65/255.0, green: 130/255.0, blue: 235/255.0, alpha: 1)
               
            } else {
                self.replyView.backgroundColor = UIColor(red: 246/255.0, green: 246/255.0, blue: 249/255.0, alpha: 1)
                
                if (model.answer.count > 0) {
                    nickNameColor = UIColor(red: 168/255.0, green: 178/255.0, blue: 188/255.0, alpha: 0.6)
                    msgColor = UIColor(red: 20/255.0, green: 23/255.0, blue: 26/255.0, alpha: 0.5)
                   
                } else {
                    msgColor = UIColor(red: 24/255.0, green: 24/255.0, blue: 24/255.0, alpha: 1)
                    
                }
               
                nickNameColor = UIColor(red: 168/255.0, green: 178/255.0, blue: 188/255.0, alpha: 0.6)
         
                
                replyNicknameColor = UIColor(red: 150/255.0, green: 160/255.0, blue: 170/255.0, alpha: 1)
                replymsgColor = .white
            }
           
           
        }
        if self.replyView.subviews.count>0{
          self.replyView.subviews.forEach({$0.removeFromSuperview()})
        }
        
        self.nameLab.font = UIFont.PingFang(size: 14)
        self.nameLab.numberOfLines = 0
        self.nameLab.lineBreakMode = .byCharWrapping;
        self.replyView.addSubview(self.nameLab)
        
    
        self.avater.contentMode = .scaleAspectFit;
        self.replyView.addSubview(self.avater)
        if (self.isQuestion) {
            self.avater.sd_setImage(with: NSURL.init(string: model.avatar) as URL?)
           
        } else {
            self.avater.image = UIImage.init(named: "HTLive.bundle/chat_tag_ask")
         

        }
        let nickname = model.nickname + " "
        let str = (nickname + model.content) as String
        
        let AttributedString = NSMutableAttributedString.init(string: str)
        
      
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.PingFang(size: 14),.foregroundColor: msgColor, ]
        
        AttributedString.addAttributes(attr, range: NSRange(location: 0, length: str.count))
        
        
        let attr1: [NSAttributedString.Key : Any] = [.font: UIFont.PingFang(size: 14),.foregroundColor: nickNameColor, ]
        
        AttributedString.addAttributes(attr1, range: NSRange(location: 0, length: nickname.count))
     
        let size = TalkfunSharedTools.getSizeLabel(labelString: AttributedString, labelWidth:self.cellwidth-35-16-12-16, labelfont: UIFont.PingFang(size: 14))
        
        self.avater.frame = CGRect(x: 6, y: 6, width: 20, height: 20)
       
        self.nameLab.frame = CGRect(x: self.avater.frame.maxX+8, y: 6, width: size.width, height: size.height)
        self.nameLab.attributedText = AttributedString;
        
        if (model.answer.count > 0) {
            var height:CGFloat = 0
            var anserwidth:CGFloat = 0
            for i in 0..<model.answer.count {

                let answer = model.answer[i] as! TFQuestionAnswer
                let img = UIImageView()
                if (self.isQuestion) {
                    img.sd_setImage(with: NSURL.init(string: answer.avatar) as URL?)
            
                } else {
                    img.image = UIImage.init(named: "HTLive.bundle/chat_tag_answer")
                 
                }
         
                let lab = UILabel()
                lab.numberOfLines = 0;
                lab.lineBreakMode = .byCharWrapping;
                let line = UILabel()
                if (self.isOrientationLandscape) {
                    line.backgroundColor = UIColor(red: 244/255.0, green: 244/255.0, blue: 248/255.0, alpha: 0.2)
                   
                } else {
                    line.backgroundColor = UIColor(red: 244/255.0, green: 244/255.0, blue: 248/255.0, alpha: 1)
                }
               
                self.replyView.addSubview(line)
                self.replyView.addSubview(img)
                self.replyView.addSubview(lab)
                
                let  str = (answer.nickname + answer.content) as String
                
                let AttributedString = NSMutableAttributedString.init(string: str)
                
                let font14 = UIFont.PingFang(size: 14)
                //-----------------
                let attr: [NSAttributedString.Key : Any] = [.font: font14,.foregroundColor: replymsgColor, ]
                AttributedString.addAttributes(attr, range: NSRange(location: 0, length: str.count))
                //-----------------
                let replyNickNameattr: [NSAttributedString.Key : Any] = [.font: font14,.foregroundColor: replyNicknameColor, ]
                
                AttributedString.addAttributes(replyNickNameattr, range: NSRange(location: 0, length: answer.nickname.count))
                
             
                let attch = NSTextAttachment()
                if (answer.role == .roleAdmin) { //助教
                    attch.image = UIImage.init(named: "HTLive.bundle/chat_tag_assistant")
                    

                } else if (answer.role == .roleSpadmin) { //老师
                    attch.image = UIImage.init(named: "HTLive.bundle/chat_tag_teacher")
                   
                }
                
                attch.bounds = CGRect(x: 0, y: -3, width: 27, height: 15)
                
                let imageStr = NSAttributedString.init(attachment: attch)
                
               

                AttributedString.insert(NSAttributedString.init(string: "  "), at: answer.nickname.count)
              
                AttributedString.insert(imageStr, at:  answer.nickname.count+" ".count)
                
                AttributedString.addAttribute(NSAttributedString.Key.kern, value: (5), range: NSRange(location: (answer.nickname as NSString).length+2, length: 1))
        
            let answersize = TalkfunSharedTools.getSizeLabel(labelString: AttributedString, labelWidth:self.cellwidth-35-16-12-16, labelfont: UIFont.PingFang(size: 14))
                
              let answersizeWidth = answersize.width as CGFloat
               //取最大值

               if answersizeWidth > anserwidth {
                    anserwidth = answersizeWidth
                }
                                              
                line.frame = CGRect(x: 6, y: 10+size.height+height, width: anserwidth+28, height: 1)
              
                img.frame = CGRect(x: 6, y: line.frame.maxY+4, width: 20, height: 20)
               
                lab.frame = CGRect(x: img.frame.maxX+8, y: line.frame.maxY+4, width: answersize.width, height: answersize.height)

                height = height+answersize.height+10
                lab.attributedText = AttributedString
                
            }
                                              
                                              
                                              
            let k = size.width>anserwidth ?size.width:anserwidth as CGFloat
            
            if (self.isOrientationLandscape) {
                self.replyView.frame = CGRect(x: 12, y: 4, width: k+16+35, height: size.height+10+1+height)
               
            } else {
                self.replyView.frame = CGRect(x: 12, y: 8, width: k+16+35, height: size.height+10+1+height)
                
            }
     
             self.replyView.addSubview(self.nameLab)
             self.replyView.addSubview(self.avater)
        
        } else {
            
            if (self.isOrientationLandscape) {
                self.replyView.frame = CGRect(x:12 , y: 4, width: size.width+16+35, height: size.height+12)
              
            } else {
                self.replyView.frame = CGRect(x: 12, y: 8, width: size.width+16+35, height: size.height+12)
           
            }
            
           
        }

        
    }
    
}
